package com.kaili.basic.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.MdmParamDto;
import com.kaili.basic.pojo.entity.MdmParamEntity;
import com.kaili.basic.pojo.vo.MdmParamVo;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kaili.basic.service.IParamService ;

import java.util.List;


/**
*  控制器
* @author:liuyang
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "参数管理", description = "参数管理相关设置API")
public class MdmParamController {
    @Autowired
    IParamService paramService;

	@GetMapping(value = "/params/{id}")
	@ApiOperation("根据ID查询")
	public MdmParamVo queryById(@ApiParam("ID") @PathVariable("id") Integer id) {
		return paramService.queryById(id);
	}

    @GetMapping(value = "/params")
    @ApiOperation("分页查询")
    public Page<MdmParamVo> queryPageList(@ModelAttribute MdmParamDto dto) {
        return paramService.queryPageList(new Page<MdmParamVo>(dto.getPage(), dto.getPageSize()), dto);
    }

    @PostMapping(value = "/params")
    @ApiOperation("新增")
    public RestStatus add(@RequestBody MdmParamEntity entity) {
        Wrapper<MdmParamEntity> wrapper = new EntityWrapper<>();
        wrapper.where("param_name={0}", entity.getParamName());
        List<Object> list = paramService.selectObjs(wrapper);
        if(list!=null && list.size()>0){
            return CommonStatus.ACC_ADD_FAIL;
        }

        paramService.insert(entity);

        return CommonStatus.ADD_OK;
    }

    @PutMapping(value = "/params/{id}")
    @ApiOperation("修改")
    public RestStatus update(@RequestBody MdmParamEntity entity) {
        Wrapper<MdmParamEntity> wrapper = new EntityWrapper<>();
        wrapper.where("param_name={0} and id!={1}", entity.getParamName(),entity.getId());
        List<Object> list = paramService.selectObjs(wrapper);
        if(list!=null && list.size()>0){
            return CommonStatus.ACC_ADD_FAIL;
        }

        paramService.updateById(entity);

        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping(value = "/params/{id}")
    @ApiOperation("删除")
    public RestStatus del(@ApiParam("ID") @PathVariable("id") Integer id) {
        MdmParamEntity entity = new MdmParamEntity();
        entity.setId(id);
        entity.setRdStatus("0");

        paramService.updateById(entity);

        return CommonStatus.DELETE_OK;
    }
}
